package com.sfh.cpc.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.sfh.cpc.com.entity.EConsume;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.entity.RxEventVip;
import com.sfh.cpc.com.req.ReqConsumeWealth;
import com.sfh.cpc.com.req.ReqUserActivateVip;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.user.model.UserModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleActivity;
import com.sfh.cpc.R;
public class UserVipActionActivity extends AbstractLifecycleActivity<UserModel> {



    public static void startUI(Context context, long dataId, int type) {

        Intent intent = new Intent(context, UserVipActionActivity.class);
        intent.putExtra("ID", dataId);
        intent.putExtra("TYPE", type);
        context.startActivity(intent);
    }

    private WidgetBaseToolbar toolbar;
    private EditText etCode;
    private LinearLayout lyCode;
    private LinearLayout lyConsume;
    private TextView tvTitle;
    private TextView tvConsumeWealth;
    private TextView tvUserWealth;
    private TextView tvToolbarTitle;
    private Switch switch1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_vip_action_activity);
        toolbar = findViewById(R.id.toolbar);
        etCode = findViewById(R.id.etCode);
        tvToolbarTitle = toolbar.setTitle("激活");

        lyCode = findViewById(R.id.ly_code);
        lyConsume = findViewById(R.id.ly_consume);
        tvTitle = findViewById(R.id.tv_title);
        tvConsumeWealth = findViewById(R.id.tv_consume_wealth);
        tvUserWealth = findViewById(R.id.tv_user_wealth);
        switch1 = findViewById(R.id.switch1);

        final long dataId = getIntent().getLongExtra("ID", -1L);
        final int type = getIntent().getIntExtra("TYPE", -1);
        ;
        if (dataId != -1) {

            switch1.setEnabled(true);
            switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        TextView tv1 = findViewById(R.id.tv_1);
                        tv1.setTextColor(Color.parseColor("#999999"));

                        TextView tv2 = findViewById(R.id.tv_2);
                        tv2.setTextColor(Color.parseColor("#2E8B57"));

                        tvToolbarTitle.setText("兑换");
                        lyConsume.setVisibility(View.VISIBLE);
                        lyCode.setVisibility(View.GONE);

                        getViewModel().queryConsume(dataId,type);
                    } else {

                        TextView tv1 = findViewById(R.id.tv_1);
                        tv1.setTextColor(Color.parseColor("#2E8B57"));

                        TextView tv2 = findViewById(R.id.tv_2);
                        tv2.setTextColor(Color.parseColor("#999999"));

                        tvToolbarTitle.setText("激活");
                        lyConsume.setVisibility(View.GONE);
                        lyCode.setVisibility(View.VISIBLE);
                    }
                }
            });

        } else {
            tvToolbarTitle.setText("激活");
            switch1.setEnabled(false);
        }
        findViewById(R.id.bt_consume_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //兑换
                getViewModel().consume(new ReqConsumeWealth(dataId, type));
            }
        });
        findViewById(R.id.btOK).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(etCode.getText().toString())) {
                    showToast("激活码不能为空");
                    return;
                }
                ReqUserActivateVip req = new ReqUserActivateVip();
                req.setCode(etCode.getText().toString());
                getViewModel().action(req);
            }
        });

    }

    @LiveDataMatch
    public void onConsumeSuccess(EConsume consume){
        tvUserWealth.setText(String.valueOf(consume.getUserWealth()));
        if (consume.getNote() != null){
            tvTitle.setText(consume.getNote().getNoteContent());
            tvConsumeWealth.setText(String.format("财富值：%s",consume.getNote().getNoteConsumeWealth()));
            if(consume.getUserWealth() >= consume.getNote().getNoteConsumeWealth()){
                //可
                tvUserWealth.setTextColor(Color.parseColor("#2E8B57"));
                findViewById(R.id.bt_consume_ok).setEnabled(true);
            }else{
                //不可
                tvUserWealth.setTextColor(Color.parseColor("#FF3030"));
                findViewById(R.id.bt_consume_ok).setEnabled(false);
            }
        }else if (consume.getMass() != null){
            tvTitle.setText(consume.getMass().getMassTitle());
            tvConsumeWealth.setText(String.format("财富值：%s",consume.getMass().getMassConsumeWealth()));
            if(consume.getUserWealth() >= consume.getMass().getMassConsumeWealth()){
                //可
                tvUserWealth.setTextColor(Color.parseColor("#2E8B57"));
                findViewById(R.id.bt_consume_ok).setEnabled(true);
            }else{
                //不可
                tvUserWealth.setTextColor(Color.parseColor("#FF3030"));
                findViewById(R.id.bt_consume_ok).setEnabled(false);
            }
        }

    }

    @LiveDataMatch
    public void onSuccess(RspBase data) {
        postEvent(new RxEventVip());
        showToast(switch1.isChecked() ? "兑换成功" : "激活成功");
        finish();
    }


}
